package com.xjtu.conan.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.conan.entity.NewsCommentsInfo;
import com.xjtu.conan.entity.TheatreCommentsInfo;
import com.xjtu.conan.exception.MyException;
import com.xjtu.conan.mapper.NewsCommentsInfoMapper;
import com.xjtu.conan.service.NewsCommentsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 新闻评论表 服务实现类
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@Service
public class NewsCommentsInfoServiceImpl extends ServiceImpl<NewsCommentsInfoMapper, NewsCommentsInfo> implements NewsCommentsInfoService {

    @Autowired
    private NewsCommentsInfoMapper commentsInfoMapper;
    @Override
    public List<NewsCommentsInfo> getAllNewsCommentsInfo(int current, int size) {
        Page<NewsCommentsInfo> page = new Page<>(current, size);
        commentsInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public NewsCommentsInfo getNewsCommentByid(Long tcid) {
        NewsCommentsInfo newsComments = commentsInfoMapper.selectById(tcid);
        if (newsComments == null) {
            throw new MyException(500, "不存在该新闻评论！");
        }
        return newsComments;
    }

    @Override
    public int addNewsComments(NewsCommentsInfo newscommentsInfo) throws IOException {
        try {
            return commentsInfoMapper.insert(newscommentsInfo);
        } catch (Exception e) {
            throw new MyException(500, "新闻评论插入失败！");
        }
    }

    @Override
    public List<NewsCommentsInfo> getNewsCommentsBynid(Long nid) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("news_id", nid);
            return commentsInfoMapper.selectByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "该新闻评论数为0！");
        }
    }

    @Override
    public List<NewsCommentsInfo> getNewsCommentsBypid(Long pid) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("parent_id", pid);
            return commentsInfoMapper.selectByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "该评论的子评论数为0！");
        }
    }

    @Override
    public int deleteNewsComments(Long ncid) {
        try {
            return commentsInfoMapper.deleteById(ncid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "新闻评论删除失败！");
        }

    }

    @Override
    public int updateNewsComments(NewsCommentsInfo newscommentsInfo) {
        try {
            return commentsInfoMapper.updateById(newscommentsInfo);
        } catch (Exception e) {
            throw new MyException(500, "新闻评论更新失败！");
        }
    }


    @Override
    public int getTotal() {
        return commentsInfoMapper.selectCount(null);
    }
}
